#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#pragma execution_character_set("utf-8")

#include <QtMultimedia>
#include <QMediaPlayer>
#include <QAudioOutput>
const int BLOCK_SIZE=25;
const int MARGIN=5;
const int AREA_ROW=20;
const int AREA_COL=15;
enum Direction
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    SPACE
};
struct Border
{
    int ubound;
    int dbound;
    int lbound;
    int rbound;
};
struct block_point
{
    int pos_x;
    int pos_y;
};

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE


class Widget : public QWidget
{
    Q_OBJECT

public:
    void InitGameFunc(); //初始化
    void StartGameFunc(); //开始游戏
    void GameOverFunc(); //游戏结束

    void ResetBlock(); //重置方块
    void BlockMove(Direction dir); //方块变动
    void BlockRotate(int block[4][4]); //方块旋转
    void CreateBlock(int block[4][4],int block_id); //产生方块
    void GetBorder(int block[4][4],Border &border); //计算边界
    void ConvertStable(int x,int y); //转换为稳定方块
    bool IsCollide(int x,int y,Direction dir);



public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QMediaPlayer *m_player = nullptr;
    QAudioOutput *m_audioOutput = nullptr;
    virtual void paintEvent(QPaintEvent *event); //刷新场景
    virtual void timerEvent(QTimerEvent *event);
    virtual void keyPressEvent(QKeyEvent *event);

private:
    Ui::Widget *ui;
private:
    int game_area[AREA_ROW][AREA_COL];
    block_point block_pos;
    Border cur_border;
    int cur_block[4][4];
    int next_block[4][4];
    bool isStable;
    int score;
    int game_timer;
    int paint_timer;
    int speed_ms;
    int refresh_ms;


};
#endif // WIDGET_H
